home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 November / CPNL0711.ISO / boekhoud / finan / BADGER finance v1.0 beta 2.exe / xampplite / phpMyAdmin / libraries / export / htmlword.php < prev    next >
PHP Script  |  2006-01-19  |  11KB  |  324 lines

  1. <?php
  2. /* $Id: htmlword.php,v 1.7 2006/01/19 15:39:29 cybot_tm Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5. /**
  6.  * Set of functions used to build CSV dumps of tables
  7.  */
  8.  
  9. /**
  10.  * Outputs comment
  11.  *
  12.  * @param   string      Text of comment
  13.  *
  14.  * @return  bool        Whether it suceeded
  15.  */
  16. function PMA_exportComment($text) {
  17.     return TRUE;
  18. }
  19.  
  20. /**
  21.  * Outputs export footer
  22.  *
  23.  * @return  bool        Whether it suceeded
  24.  *
  25.  * @access  public
  26.  */
  27. function PMA_exportFooter() {
  28.     return PMA_exportOutputHandler('</body></html>');
  29. }
  30.  
  31. /**
  32.  * Outputs export header
  33.  *
  34.  * @return  bool        Whether it suceeded
  35.  *
  36.  * @access  public
  37.  */
  38. function PMA_exportHeader() {
  39.     global $charset, $charset_of_file;
  40.     return PMA_exportOutputHandler('<html xmlns:o="urn:schemas-microsoft-com:office:office"
  41. xmlns:x="urn:schemas-microsoft-com:office:word"
  42. xmlns="http://www.w3.org/TR/REC-html40">
  43.  
  44. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  45. <html>
  46. <head>
  47.     <meta http-equiv="Content-type" content="text/html;charset=' . ( isset($charset_of_file) ? $charset_of_file : $charset ) .'" />
  48. </head>
  49. <body>');
  50. }
  51.  
  52. /**
  53.  * Outputs database header
  54.  *
  55.  * @param   string      Database name
  56.  *
  57.  * @return  bool        Whether it suceeded
  58.  *
  59.  * @access  public
  60.  */
  61. function PMA_exportDBHeader($db) {
  62.     return PMA_exportOutputHandler('<h1>' . $GLOBALS['strDatabase'] . ' ' . $db . '</h1>');
  63. }
  64.  
  65. /**
  66.  * Outputs database footer
  67.  *
  68.  * @param   string      Database name
  69.  *
  70.  * @return  bool        Whether it suceeded
  71.  *
  72.  * @access  public
  73.  */
  74. function PMA_exportDBFooter($db) {
  75.     return TRUE;
  76. }
  77.  
  78. /**
  79.  * Outputs create database database
  80.  *
  81.  * @param   string      Database name
  82.  *
  83.  * @return  bool        Whether it suceeded
  84.  *
  85.  * @access  public
  86.  */
  87. function PMA_exportDBCreate($db) {
  88.     return TRUE;
  89. }
  90.  
  91. /**
  92.  * Outputs the content of a table in CSV format
  93.  *
  94.  * @param   string      the database name
  95.  * @param   string      the table name
  96.  * @param   string      the end of line sequence
  97.  * @param   string      the url to go back in case of error
  98.  * @param   string      SQL query for obtaining data
  99.  *
  100.  * @return  bool        Whether it suceeded
  101.  *
  102.  * @access  public
  103.  */
  104. function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
  105. {
  106.     global $what;
  107.  
  108.     if (!PMA_exportOutputHandler('<h2>' . $GLOBALS['strDumpingData'] . ' ' . $table . '</h2>')) {
  109.         return FALSE;
  110.     }
  111.     if (!PMA_exportOutputHandler('<table class="width100" cellspacing="1">')) {
  112.         return FALSE;
  113.     }
  114.  
  115.     // Gets the data from the database
  116.     $result      = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
  117.     $fields_cnt  = PMA_DBI_num_fields($result);
  118.  
  119.     // If required, get fields name at the first line
  120.     if (isset($GLOBALS[$what . '_shownames']) && $GLOBALS[$what . '_shownames'] == 'yes') {
  121.         $schema_insert = '<tr class="print-category">';
  122.         for ($i = 0; $i < $fields_cnt; $i++) {
  123.             $schema_insert .= '<td class="print"><b>' . htmlspecialchars(stripslashes(PMA_DBI_field_name($result, $i))) . '</b></td>';
  124.         } // end for
  125.         $schema_insert .= '</tr>';
  126.         if (!PMA_exportOutputHandler($schema_insert)) {
  127.             return FALSE;
  128.         }
  129.     } // end if
  130.  
  131.     // Format the data
  132.     while ($row = PMA_DBI_fetch_row($result)) {
  133.         $schema_insert = '<tr class="print-category">';
  134.         for ($j = 0; $j < $fields_cnt; $j++) {
  135.             if (!isset($row[$j]) || is_null($row[$j])) {
  136.                 $value = $GLOBALS[$what . '_replace_null'];
  137.             } elseif ($row[$j] == '0' || $row[$j] != '') {
  138.                 $value = $row[$j];
  139.             } else {
  140.                 $value = '';
  141.             }
  142.             $schema_insert .= '<td class="print">' . htmlspecialchars($value) . '</td>';
  143.         } // end for
  144.         $schema_insert .= '</tr>';
  145.         if (!PMA_exportOutputHandler($schema_insert)) {
  146.             return FALSE;
  147.         }
  148.     } // end while
  149.     PMA_DBI_free_result($result);
  150.     if (!PMA_exportOutputHandler('</table>')) {
  151.         return FALSE;
  152.     }
  153.  
  154.     return TRUE;
  155. }
  156.  
  157. function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false)
  158. {
  159.     global $cfgRelation;
  160.  
  161.     if (!PMA_exportOutputHandler('<h2>' . $GLOBALS['strTableStructure'] . ' ' .$table . '</h2>')) {
  162.         return FALSE;
  163.     }
  164.  
  165.     /**
  166.      * Get the unique keys in the table
  167.      */
  168.     $keys_query     = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM '. PMA_backquote($db);
  169.     $keys_result    = PMA_DBI_query($keys_query);
  170.     $unique_keys    = array();
  171.     while ($key = PMA_DBI_fetch_assoc($keys_result)) {
  172.         if ($key['Non_unique'] == 0) {
  173.             $unique_keys[] = $key['Column_name'];
  174.         }
  175.     }
  176.     PMA_DBI_free_result($keys_result);
  177.  
  178.     /**
  179.      * Gets fields properties
  180.      */
  181.     PMA_DBI_select_db($db);
  182.     $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
  183.     $result      = PMA_DBI_query($local_query);
  184.     $fields_cnt  = PMA_DBI_num_rows($result);
  185.  
  186.     // Check if we can use Relations (Mike Beck)
  187.     if ($do_relation && !empty($cfgRelation['relation'])) {
  188.         // Find which tables are related with the current one and write it in
  189.         // an array
  190.         $res_rel = PMA_getForeigners($db, $table);
  191.  
  192.         if ($res_rel && count($res_rel) > 0) {
  193.             $have_rel = TRUE;
  194.         } else {
  195.             $have_rel = FALSE;
  196.         }
  197.     } else {
  198.            $have_rel = FALSE;
  199.     } // end if
  200.  
  201.     /**
  202.      * Displays the table structure
  203.      */
  204.     if (!PMA_exportOutputHandler('<table class="width100" cellspacing="1">')) {
  205.         return FALSE;
  206.     }
  207.  
  208.     $columns_cnt = 4;
  209.     if ($do_relation && $have_rel) {
  210.         $columns_cnt++;
  211.     }
  212.     if ($do_comments && $cfgRelation['commwork']) {
  213.         $columns_cnt++;
  214.     }
  215.     if ($do_mime && $cfgRelation['mimework']) {
  216.         $columns_cnt++;
  217.     }
  218.  
  219.     $schema_insert = '<tr class="print-category">';
  220.     $schema_insert .= '<th class="print">' . htmlspecialchars($GLOBALS['strField']) . '</th>';
  221.     $schema_insert .= '<td class="print"><b>' . htmlspecialchars($GLOBALS['strType']) . '</b></td>';
  222.     $schema_insert .= '<td class="print"><b>' . htmlspecialchars($GLOBALS['strNull']) . '</b></td>';
  223.     $schema_insert .= '<td class="print"><b>' . htmlspecialchars($GLOBALS['strDefault']) . '</b></td>';
  224.     if ($do_relation && $have_rel) {
  225.         $schema_insert .= '<td class="print"><b>' . htmlspecialchars($GLOBALS['strLinksTo']) . '</b></td>';
  226.     }
  227.     if ($do_comments && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) {
  228.         $schema_insert .= '<td class="print"><b>' . htmlspecialchars($GLOBALS['strComments']) . '</b></td>';
  229.         $comments = PMA_getComments($db, $table);
  230.     }
  231.     if ($do_mime && $cfgRelation['mimework']) {
  232.         $schema_insert .= '<td class="print"><b>' . htmlspecialchars('MIME') . '</b></td>';
  233.         $mime_map = PMA_getMIME($db, $table, true);
  234.     }
  235.     $schema_insert .= '</tr>';
  236.  
  237.     if (!PMA_exportOutputHandler($schema_insert)) {
  238.         return FALSE;
  239.     }
  240.  
  241.     while ($row = PMA_DBI_fetch_assoc($result)) {
  242.  
  243.         $schema_insert = '<tr class="print-category">';
  244.         $type             = $row['Type'];
  245.         // reformat mysql query output - staybyte - 9. June 2001
  246.         // loic1: set or enum types: slashes single quotes inside options
  247.         if (eregi('^(set|enum)\((.+)\)$', $type, $tmp)) {
  248.             $tmp[2]       = substr(ereg_replace('([^,])\'\'', '\\1\\\'', ',' . $tmp[2]), 1);
  249.             $type         = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
  250.             $type_nowrap  = '';
  251.  
  252.             $binary       = 0;
  253.             $unsigned     = 0;
  254.             $zerofill     = 0;
  255.         } else {
  256.             $type_nowrap  = ' nowrap="nowrap"';
  257.             $type         = eregi_replace('BINARY', '', $type);
  258.             $type         = eregi_replace('ZEROFILL', '', $type);
  259.             $type         = eregi_replace('UNSIGNED', '', $type);
  260.             if (empty($type)) {
  261.                 $type     = ' ';
  262.             }
  263.  
  264.             $binary       = eregi('BINARY', $row['Type']);
  265.             $unsigned     = eregi('UNSIGNED', $row['Type']);
  266.             $zerofill     = eregi('ZEROFILL', $row['Type']);
  267.         }
  268.         $strAttribute     = ' ';
  269.         if ($binary) {
  270.             $strAttribute = 'BINARY';
  271.         }
  272.         if ($unsigned) {
  273.             $strAttribute = 'UNSIGNED';
  274.         }
  275.         if ($zerofill) {
  276.             $strAttribute = 'UNSIGNED ZEROFILL';
  277.         }
  278.         if (!isset($row['Default'])) {
  279.             if ($row['Null'] != '') {
  280.                 $row['Default'] = 'NULL';
  281.             }
  282.         } else {
  283.             $row['Default'] = $row['Default'];
  284.         }
  285.  
  286.         $fmt_pre = '';
  287.         $fmt_post = '';
  288.         if (in_array($row['Field'], $unique_keys)) {
  289.             $fmt_pre = '<b>' . $fmt_pre;
  290.             $fmt_post = $fmt_post . '</b>';
  291.         }
  292.         if ($row['Key']=='PRI') {
  293.             $fmt_pre = '<i>' . $fmt_pre;
  294.             $fmt_post = $fmt_post . '</i>';
  295.         }
  296.         $schema_insert .= '<td class="print">' . $fmt_pre . htmlspecialchars($row['Field']) . $fmt_post . '</td>';
  297.         $schema_insert .= '<td class="print">' . htmlspecialchars($type) . '</td>';
  298.         $schema_insert .= '<td class="print">' . htmlspecialchars($row['Null'] == '' ? $GLOBALS['strNo'] : $GLOBALS['strYes']) . '</td>';
  299.         $schema_insert .= '<td class="print">' . htmlspecialchars(isset($row['Default']) ? $row['Default'] : '') . '</td>';
  300.  
  301.         $field_name = $row['Field'];
  302.  
  303.         if ($do_relation && $have_rel) {
  304.             $schema_insert .= '<td class="print">' . (isset($res_rel[$field_name]) ? htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')') : '') . '</td>';
  305.         }
  306.         if ($do_comments && $cfgRelation['commwork']) {
  307.             $schema_insert .= '<td class="print">' . ( isset($comments[$field_name]) ? htmlspecialchars($comments[$field_name]) : '') . '</td>';
  308.         }
  309.         if ($do_mime && $cfgRelation['mimework']) {
  310.             $schema_insert .= '<td class="print">' . ( isset($mime_map[$field_name]) ? htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype'])) : '') . '</td>';
  311.         }
  312.  
  313.         $schema_insert .= '</tr>';
  314.  
  315.         if (!PMA_exportOutputHandler($schema_insert)) {
  316.             return FALSE;
  317.         }
  318.     } // end while
  319.     PMA_DBI_free_result($result);
  320.  
  321.     return PMA_exportOutputHandler('</table>');
  322. }
  323. ?>
  324.